package com.parkcar;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import HttpUtils.HttpInfoRequest;
import util.BaseActivity;
import util.BaseTools;
import util.BaseUtil;

/**
 * Created by Administrator on 2016/1/9.
 */
public class LoginActivity extends BaseActivity {
    private ImageView select_iv, delete;
    private EditText evMobile, evVerify;
    private TextView btVerify;
    private Button btLogin;
    private boolean isSelect = true;
    private HttpInfoRequest codeRequest, loginRequest;

    @Override
    protected void setUpui(Bundle savedInstanceState) {
        setContentView(R.layout.activity_login);
        setTitle("登录");
        setTitlebg();
        settitleColor(R.color.title_bg);
        setLeftImage(R.drawable.back_green);
        initView();
    }

    @Override
    protected void updateUI(boolean isLogin) {
    }

    private void initView() {
        btVerify = (TextView) findViewById(R.id.btVerify);
        select_iv = (ImageView) findViewById(R.id.select_iv);
        evMobile = (EditText) findViewById(R.id.evMobile);
        evVerify = (EditText) findViewById(R.id.evVerify);
        delete = (ImageView) findViewById(R.id.delete);
        btLogin = (Button) findViewById(R.id.btLogin);
        evVerify.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (evVerify.getText().length() > 0) {
                    delete.setVisibility(View.VISIBLE);
                    btLogin.setBackgroundResource(R.drawable.add_bg);
                } else {
                    delete.setVisibility(View.GONE);
                    btLogin.setBackgroundResource(R.drawable.unselect_bg);
                }
            }
        });
    }

    private CountDownTimer cuntDown = new CountDownTimer(60000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            btVerify.setClickable(false);
            btVerify.setText("(" + millisUntilFinished / 1000 + ")后重新发送");
        }

        @Override
        public void onFinish() {
            btVerify.setBackgroundResource(R.drawable.code_bg);
            btVerify.setTextColor(getResources().getColor(R.color.title_bg));
            btVerify.setText("重新获取");
            btVerify.setClickable(true);
        }
    };

    public void getValidateCode(View viwe) {
        if (BaseTools.isMobileNO(evMobile.getText().toString())) {
            btVerify.setTextColor(getResources().getColor(R.color.gray9));
            btVerify.setBackgroundResource(R.drawable.resend_bg);
            cuntDown.start();
            codeRequest = new HttpInfoRequest(this, 0);
            codeRequest.getMobileCode(evMobile.getText().toString());
            codeRequest.setmIHttpClinetListener(this);
            codeRequest.doRequest();
        } else {
            toast("请输入正确的手机号码");
        }
    }

    public void toClear(View view) {
        evVerify.setText("");
        view.setVisibility(View.GONE);
    }

    public void toNextStep(View view) {
        if (!isSelect) {
            toast("请点击选择同意来停车服务协议");
            return;
        }
        if (!BaseTools.isMobileNO(evMobile.getText().toString())) {
            toast("请输入正确的电话号码");
            return;
        }
        if (BaseTools.isValueEmpty(evVerify.getText().toString())) {
            toast("请输入验证码");
            return;
        }
        loginRequest = new HttpInfoRequest(this, 1);
        loginRequest.loginIn(evMobile.getText().toString(), evVerify.getText().toString());
        loginRequest.setmIHttpClinetListener(this);
        loginRequest.doRequest();
    }

    public void select(View view) {
        isSelect = !isSelect;
        if (isSelect) {
            select_iv.setImageResource(R.drawable.selected);
        } else {
            select_iv.setImageResource(R.drawable.select_bg);
        }
    }

    @Override
    public void httpSuccessListener(Bundle successMsg) {
        super.httpSuccessListener(successMsg);
        if (codeRequest != null && action.equalsIgnoreCase(codeRequest.getAction())) {
            if (mBaseResult != null && mBaseResult.getStatus().getRetCode() == 0) {
                toast(mBaseResult.getStatus().getMsg());
            }
        } else if (loginRequest != null && action.equalsIgnoreCase(loginRequest.getAction())) {
            if (mBaseResult == null) {
                toast("与服务器失去连接");
                return;
            }
            if (mBaseResult.getStatus().getRetCode() == 0) {
                try {
                    String token = mBaseResult.getAsJsonObject().getString("token");
                    if (!BaseTools.isValueEmpty(token)) {
                        BaseUtil.setToken(this, token);
                        Intent  mIntent = new Intent();
                        mIntent.setAction("login");
                        mIntent.putExtra("login",true);
                        sendBroadcast(mIntent);
                    }
                    finish();
                } catch (Exception e) {

                }
            }
            toast(mBaseResult.getStatus().getMsg());
        }
    }
}
